package netty.iosocket;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/11/18 0018
 * Time: 下午 6:18
 * To change this template use File | Settings | File Templates.
 */
public class ServerSocketTest {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(8080);
        while (true){
            new Thread(new SocketHandler(serverSocket.accept())).start();
        }

    }

  static   class SocketHandler implements Runnable{
        private Socket socket;

        public SocketHandler(Socket socket){
            this.socket = socket;
        }

        @Override
        public void run() {
            System.out.println("thread name:"+Thread.currentThread().getName());
            try {
                InputStream inputStream = socket.getInputStream();
                byte[] bytes = new byte[1024];
                inputStream.read(bytes, 0, bytes.length);
                System.out.println("接受客户端请求参数:"+new String(bytes));
                bytes = "服务端返回 hello world".getBytes();
                socket.getOutputStream().write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
